using Os.Domain;
using Os.WebUI.ViewModels;
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Globalization;

namespace Os.WebUI.Controllers
{
	[Authorize(Roles = "Administrador")]
	public class RelatoriosController : Controller
	{
		[HttpGet]
		public ActionResult Index()
		{
			return View();
		}
		[HttpGet]
		public ActionResult Equipamentos()
		{
			ClienteEquipamentoViewModel clienteEquipamentoViewModel = new ClienteEquipamentoViewModel();
			clienteEquipamentoViewModel.clientes = Cliente.CarregarTodos();
			clienteEquipamentoViewModel.equipamentos = new List<Equipamento>();
			ViewData["Result"] = "";
			return View(clienteEquipamentoViewModel);
		}
		[HttpPost]
		public ActionResult Equipamentos(FormCollection collection, ClienteEquipamentoViewModel post)
		{
			Cliente objCliente = Cliente.CarregarObjeto(Convert.ToInt32(collection["Clientes"]));
			ClienteEquipamentoViewModel clienteEquipamentoViewModel = new ClienteEquipamentoViewModel();
			clienteEquipamentoViewModel.clientes = Cliente.CarregarTodos();
			clienteEquipamentoViewModel.localizacao = collection["Localizacao"].ToString();
			clienteEquipamentoViewModel.modelo = collection["Modelo"].ToString();
			clienteEquipamentoViewModel.equipamentos = Equipamento.CarregarRelatorio(objCliente, collection["localizacao"].ToString(), collection["modelo"].ToString());
			if (clienteEquipamentoViewModel.equipamentos.Count == 0)
			{
				ViewData["Result"] = "Não foram encontrados registros para os parâmetros informados.";
			}
			return View(clienteEquipamentoViewModel);
		}
		[HttpGet]
		public ActionResult Clientes()
		{
			ClienteReportViewModel clienteReportViewModel = new ClienteReportViewModel();
			clienteReportViewModel.Nome = string.Empty;
			clienteReportViewModel.Cnpj = string.Empty;
			clienteReportViewModel.Clientes = new List<Cliente>();
			return View(clienteReportViewModel);
		}
		[HttpPost]
		public ActionResult Clientes(FormCollection collection)
		{
			ClienteReportViewModel clienteReportViewModel = new ClienteReportViewModel();
			clienteReportViewModel.Nome = collection["Nome"].ToString();
			clienteReportViewModel.Cnpj = collection["Cnpj"].ToString();
			clienteReportViewModel.Clientes = Cliente.CarregarPorNomeECnpj(clienteReportViewModel.Nome, clienteReportViewModel.Cnpj);
			return View(clienteReportViewModel);
		}
		[HttpGet]
		public ActionResult OrdensServico()
		{
			OrdenServicoReportViewModel ObjReport = new OrdenServicoReportViewModel();

            ObjReport.DataAberturaInicial = DateTime.Now;
            ObjReport.DataAberturaFinal = DateTime.Now;
            ObjReport.Status = StatusOrdemServico.Solicitado;
            ObjReport.Cliente = new Cliente();
            ObjReport.OrdensDeServico = new List<OrdemServico>();

			ViewData["StatusOs"] = StatusOs.CarregarTodos();
			ViewData["Clientes"] = Cliente.CarregarTodos();

            return View(ObjReport);
		}
		public JsonResult LoadRelatorioOrdemServico()
		{
			OrdenServicoReportViewModel objReport = new OrdenServicoReportViewModel();
            int status = 0;
            objReport.DataAberturaInicial = Convert.ToDateTime(Request.QueryString["DataInicial"],new CultureInfo("pt-br"));
            objReport.DataAberturaFinal = Convert.ToDateTime(Request.QueryString["DataFinal"], new CultureInfo("pt-br"));
			if (Request.QueryString["Status"].ToString() != "")
			{
				status = Convert.ToInt32(Request.QueryString["Status"]);
			}
			if (Request.QueryString["Cliente"].ToString() != "Todos")
			{
                objReport.Cliente = Cliente.CarregarObjeto(Convert.ToInt32(Request.QueryString["Cliente"]));
			}
			else
			{
                objReport.Cliente = null;
			}
            List<OrdemServico> ordens = OrdemServico.CarregarRelatorio(   objReport.DataAberturaInicial, 
                                                                        objReport.DataAberturaFinal,
                                                                        status, 
                                                                        objReport.Cliente);
			return Json(ordens, JsonRequestBehavior.AllowGet);
		}
		[HttpGet]
		public ActionResult VigenciaContrato()
		{
			VigenciaViewModel vigenciaViewModel = new VigenciaViewModel();
			vigenciaViewModel.DataInicial = DateTime.Now;
			vigenciaViewModel.DataFinal = DateTime.Now;
			vigenciaViewModel.Clientes = new List<Cliente>();
			return View(vigenciaViewModel);
		}
		[HttpPost]
		public ActionResult VigenciaContrato(FormCollection collection)
		{
			VigenciaViewModel vigenciaViewModel = new VigenciaViewModel();
            if (collection["DataInicial"] != "")
            {
                vigenciaViewModel.DataInicial = Convert.ToDateTime(collection["DataInicial"], new CultureInfo("pt-br"));
            }
            else
            {
                vigenciaViewModel.DataInicial = DateTime.MinValue;
            }
            if (collection["DataFinal"] != "")
            {
                DateTime dataFinal = Convert.ToDateTime(collection["DataFinal"], new CultureInfo("pt-br"));
                if(dataFinal != DateTime.MaxValue)
                {
                    vigenciaViewModel.DataFinal = dataFinal.AddDays(1).AddSeconds(-1);
                }
                else
                {
                    vigenciaViewModel.DataFinal = DateTime.MaxValue;
                }
            }
            else
            {
                vigenciaViewModel.DataFinal = DateTime.MaxValue;
            }

			vigenciaViewModel.Clientes = Cliente.CarregarPorVigencia(vigenciaViewModel.DataInicial, vigenciaViewModel.DataFinal);


			return View(vigenciaViewModel);
		}
	}
}
